#!/bin/sh

# 检测待 commit 内容是否包括 js/css 文件
files=$(git diff --cached --name-only | grep -E '\.js$|\.css$')

if [[ $files = "" ]] ; then
    exit 0
fi

file=public/wechat/inc/config.php

if [ -e $file ] ; then
    exp="s/.*APP_VERSION.*/define('APP_VERSION', '1.0.0.$(date +%s)');/"

    if [ "$(uname)" == "Darwin" ]; then
        # Mac
        sed -i "" "$exp" $file
    elif [ "$(expr substr $(uname -s) 1 5)" == "MINGW" ]; then
        # Win
        git_path=$(where git)
        if [[ $git_path = "" ]] ; then
            echo "git not found. exit 0"
            exit 0
        fi
        
        sed="${git_path%%Git*}Git\usr\bin\sed.exe"
        echo $sed
        sed -i "$exp" $file
    fi
    
    echo "检测到 js/css 文件变动，自动更新 $file 文件 APP_VERSION 版本号变量的值"
    git add $file
fi

exit 0
